﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Uber.Collections;
using Uber.Storage;
using Uber.HabboHotel.GameClients;
using Uber.HabboHotel.Catalogs;

namespace Uber.HabboHotel.Users.Subscriptions
{
    class SubscriptionManager
    {
        private uint UserId;
        private SafeDictionary<string, Subscription> Subscriptions;

        public Boolean UnlockedNewGift
        {
            get
            {
                foreach (ClubGift Gift in UberEnvironment.GetGame().GetCatalog().GetHabboGiftPageItems())
                {
                    if (GetCurrentSub.DaysDone >= Gift.DaysNeed)
                    {
                        return true;
                    }
                }

                return false;
            }
        }

        public Subscription GetCurrentSub
        {
            get
            {
                if (IsClubOnly)
                {
                    return this.Subscriptions["habbo_club"];
                }
                else if (IsVip)
                {
                    return this.Subscriptions["club_habbo"];
                }
                else { return null; }
            }
        }

        public List<string> SubList
        {
            get
            {
                var List = new List<string>();

                foreach (Subscription Subscription in Subscriptions.Values)
                {
                    List.Add(Subscription.Type);
                }

                return List;
            }
        }

        public void BuyItem(uint ItemId, GameClient Session)
        {
            if (ItemId == 4533) // club 1 month
            {
                if (!Session.GetHabbo().GetBadgeComponent().HasBadge("ACH_BasicClub1"))
                {
                    Session.GetHabbo().GetBadgeComponent().GiveBadge("ACH_BasicClub1", true);
                }

                Session.GetHabbo().GetSubscriptionManager().AddOrExtendSubscription("habbo_club", 1);
                Session.GetMessageHandler().GetSubscriptionData();

                Session.GetHabbo().Credits = Session.GetHabbo().Credits - 5;
                Session.GetHabbo().UpdateCreditsBalance(true);
            }

            if (ItemId == 4534) // club 3 months
            {
                if (!Session.GetHabbo().GetBadgeComponent().HasBadge("ACH_BasicClub1"))
                {
                    Session.GetHabbo().GetBadgeComponent().GiveBadge("ACH_BasicClub1", true);
                }

                Session.GetHabbo().GetSubscriptionManager().AddOrExtendSubscription("habbo_club", 3);
                Session.GetMessageHandler().GetSubscriptionData();

                Session.GetHabbo().Credits = Session.GetHabbo().Credits - 45;
                Session.GetHabbo().UpdateCreditsBalance(true);
            }

            if (ItemId == 4535) // vip 1 month
            {
                if (!Session.GetHabbo().GetBadgeComponent().HasBadge("ACH_VipClub1"))
                {
                    Session.GetHabbo().GetBadgeComponent().GiveBadge("ACH_VipClub1", true);
                }

                Session.GetHabbo().GetSubscriptionManager().AddOrExtendSubscription("club_habbo", 1);
                Session.GetMessageHandler().GetSubscriptionData();

                Session.GetHabbo().Credits = Session.GetHabbo().Credits - 25;
                Session.GetHabbo().UpdateCreditsBalance(true);
            }

            if (ItemId == 4536) // vip 3 months
            {
                if (!Session.GetHabbo().GetBadgeComponent().HasBadge("ACH_VipClub1"))
                {
                    Session.GetHabbo().GetBadgeComponent().GiveBadge("ACH_VipClub1", true);
                }

                Session.GetHabbo().GetSubscriptionManager().AddOrExtendSubscription("club_habbo", 3);
                Session.GetMessageHandler().GetSubscriptionData();

                Session.GetHabbo().Credits = Session.GetHabbo().Credits - 50;
                Session.GetHabbo().UpdateCreditsBalance(true);
            }   
        }

        public SubscriptionManager(uint UserId)
        {
            this.UserId = UserId;

            Subscriptions = new SafeDictionary<string, Subscription>();
        }

        public void LoadSubscriptions()
        {
            DataTable SubscriptionData = null;

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                SubscriptionData = dbClient.ReadDataTable("SELECT * FROM user_subscriptions WHERE user_id = '" + UserId + "'");
            }

            if (SubscriptionData != null)
            {
                foreach (DataRow Row in SubscriptionData.Rows)
                {
                    Subscriptions.Add((string)Row["subscription_id"], new Subscription((string)Row["subscription_id"], (string)Row["start"], (int)Row["months"], (int)Row["times_gift"]));
                }
            }
        }

        public void Clear()
        {
            Subscriptions.Clear();
        }

        public Subscription GetSubscription(string SubscriptionId)
        {
            if (Subscriptions.ContainsKey(SubscriptionId))
            {
                return Subscriptions[SubscriptionId];
            }

            return null;
        }

        public Boolean HasSubscription(string SubscriptionId)
        {
            if (!Subscriptions.ContainsKey(SubscriptionId))
            {
                return false;
            }

            Subscription Sub = Subscriptions[SubscriptionId];

            if (Sub.IsValid())
            {
                return true;
            }

            return false;
        }

        public Boolean IsVip
        {
            get
            {
                return (HasSubscription("club_habbo"));
            }
        }

        public Boolean IsClubOnly
        {
            get
            {
                return (!HasSubscription("club_habbo") && HasSubscription("habbo_club"));
            }
        }

        public void AddOrExtendSubscription(string SubscriptionId, int Months)
        {
            SubscriptionId = SubscriptionId.ToLower();

            if (Subscriptions.ContainsKey(SubscriptionId))
            {
                using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                {
                    dbClient.ExecuteQuery("UPDATE user_subscriptions SET months = months + " + Months + " WHERE user_id = '" + UserId + "' AND subscription_id = '" + SubscriptionId + "' LIMIT 1");
                }

                return;
            }

            string Start = DateTime.Now.ToString();

            Subscription NewSub = new Subscription(SubscriptionId, Start, Months, 0);

            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("INSERT INTO user_subscriptions (user_id,subscription_id,start,months) VALUES ('" + UserId + "','" + SubscriptionId + "','" + Start + "','" + Months + "')");
            }

            Subscriptions.Add(NewSub.Type.ToLower(), NewSub);
        }
    }
}
